package storage;

import general.Flag;
import general.Status;

import java.util.Map;

/**
 * Base and default implementation of StorageAdapter interface
 * 
 * @author Last G, ld
 */
public class Storage implements StorageAdapter
{
	// private Map<String, Map<String, String>> config;
	private StorageAdapter realStorage;

	/** creates a Storage object for flags
	 * @param config configurations
	 * @throws StorageException can't create storage with this configs
	 * @author Last G
	 */
	public Storage(Map<String, Map<String, String>> config)
			throws StorageException
	{
		// this.config = config;
		String dbType = config.get("STORAGE").get("DB_TYPE").toUpperCase();
		if (dbType.equals("MEMORY"))
		{
			realStorage = new Memory(config);
		} else
		{
			throw new StorageException("Incorrect configs: unknown DB Type:"
					+ dbType);
		}
	}

	/**
	 * 
	 * @see storage.StorageAdapter#getFlags(int)
	 */
	@Override
	public Flag getFlag(int id) throws StorageException
	{
		return realStorage.getFlag(id);
	}

	/**
	 * 
	 * @see storage.StorageAdapter#getFlags(general.Status, int)
	 */
	@Override
	public Flag[] getFlags(Status state, int limit) throws StorageException
	{
		return realStorage.getFlags(state, limit);
	}

	/**
	 * @see storage.StorageAdapter#putFlag(general.Flag)
	 */
	public int putFlag(Flag fl) throws StorageException
	{
		return realStorage.putFlag(fl);
	}

	/**
	 * @see storage.StorageAdapter#updateStatus(general.Flag[], general.Status)
	 */
	@Override
	public void updateStatus(Flag[] flags, Status[] statuses)
			throws StorageException
	{
		realStorage.updateStatus(flags, statuses);

	}

	@Override
	public Flag getFlagAndLock(int id) throws StorageException
	{
		return realStorage.getFlagAndLock(id);
	}

	@Override
	public Flag[] getFlagsAndLock(Status state, int limit)
			throws StorageException
	{
		return realStorage.getFlagsAndLock(state, limit);
	}

}
